<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'basic';
$return=array();
/*******basic*******/
if($_RQ['op']=='basic'){
	$pluginname=$_RQ['pluginname'];
	$code=$_RQ['code'];
	if($code){
		$item=pdo_get('core_open_plugin',array('pid'=>$_RQ['pid'],'uniacid'=>0,'isplugin'=>0));
		$content=model('core/wxwork_api',$item['id'])->get_permanent_code($code);
		cfc('wxwork_app')->upitem($_SESSION['merchid'],$content,'',$item['id']);
	}
	if(!$pluginname){
		exi('请选择应用进入！','error');
	}
	$return['up2workurl']=manage_url('core/wxwork.authapp/up2workurl/pluginname'.$pluginname,true);
	exi($return);
}
if($_RQ['op']=='up2workurl'){
	$pluginname=$_RQ['pluginname'];
	$pid=pdo_getcolumn('core_plugins',array('name'=>$pluginname),'id');
	$item=pdo_get('core_open_plugin',array('pid'=>$pid,'uniacid'=>0,'isplugin'=>0));
	if(!$item['suite_id'] || !$item['suite_secret']){
		exi('该应用还未绑定企业微信平台的应用ID或应用secret！请联系管理员完善！','error');
	}
	if($item['id']){
		$pre_auth_code=model('core/wxwork_api',$item['id'])->get_pre_auth_code();
		model('core/wxwork_api',$item['id'])->set_session_info($pre_auth_code);
	}
	$backurl=urlencode(manage_url('core/wxwork.authapp//pid='.$item['id'],true));
	$state=session_id();
	$up2workurl='https://open.work.weixin.qq.com/3rdapp/install?suite_id='.$item['suite_id'].'&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$backurl.'&state='.$state;
	$return['authurl']=$up2workurl;
	$return['pre_auth_code']=$pre_auth_code;
	exi('','',$up2workurl);
}
